<?php
session_start();
require_once("include/db.php");

if (isset($_SESSION['user_id'])) {
    header("HTTP/1.1 302 Moved Temporarily");
    header("Location: ./");
    exit();
}

if (isset($_POST['email']) && isset($_POST['password']))
{
    session_regenerate_id(true);
    $user = login_user($_POST['email'], $_POST['password']);
    if ($user !== false)
    {
        $_SESSION['user_id'] = $user['ID'];
        $_SESSION['user_name'] = $user['Name'];
        $_SESSION['user_email'] = $user['Email'];
        header("HTTP/1.1 302 Moved Temporarily");
        header("Location: ./");
        exit();
    }
    $error = 'Login incorrect';
}

$title = 'Login';
require("include/header.php");
?>

<!-- Begin page content -->
<div class="container">
    <div class="page-header">
        <h1>Login</h1>
    </div>
    <p style="color: red"><?php echo @$error; ?></p>
    <form class="form-signin" role="form" method="POST">
        <h2 class="form-signin-heading">Please login</h2>
        <input name="email" type="text" class="form-control" placeholder="Username (Email)" required autofocus>
        <input name="password" type="password" class="form-control" placeholder="Password" required>

        <input class="btn btn-lg btn-primary btn-block" id="btn" type="submit" value="Login">
    </form>
</div>

<?php require("include/footer.php"); ?>
